package com.platform.usercenter.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.platform.common.core.utils.StringUtils;
import com.platform.usercenter.domain.SysUserAirport;
import com.platform.usercenter.domain.dto.SelectOptionDTO;
import com.platform.usercenter.mapper.SysUserAirportMapper;
import com.platform.usercenter.service.ISysUserAirportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户与机场关联Service业务层处理
 *
 * @author platform
 * @date 2023-03-30
 */
@Service
public class SysUserAirportServiceImpl implements ISysUserAirportService {
    @Resource
    private SysUserAirportMapper sysUserAirportMapper;

    /**
     * 查询用户与机场关联
     *
     * @param userId 用户与机场关联主键
     * @return 用户与机场关联
     */
    @Override
    public List<SysUserAirport> selectSysUserAirportByUserId(Long userId) {
        return sysUserAirportMapper.selectSysUserAirportByUserId(userId);
    }

    /**
     * 查询用户与机场关联列表
     *
     * @param sysUserAirport 用户与机场关联
     * @return 用户与机场关联
     */
    @Override
    public List<SysUserAirport> selectSysUserAirportList(SysUserAirport sysUserAirport) {
        return sysUserAirportMapper.selectSysUserAirportList(sysUserAirport);
    }

    /**
     * 查询用户与机场关联列表
     *
     * @param sysUserAirport 用户与机场关联
     * @return 用户与机场关联
     */
    @Override
    public List<SysUserAirport> selectSysUserAirportListByRole(SysUserAirport sysUserAirport) {
        List<SysUserAirport> sysUserAirports = new ArrayList<>();
        // 查询的数据为json格式，需要处理为对应字段
        Set<String> airportList = new HashSet<>();
        List<SysUserAirport> userAirports=sysUserAirportMapper.selectSysUserAirportListByRole(sysUserAirport);
        if(CollectionUtil.isNotEmpty(userAirports)){
            for (SysUserAirport airport : userAirports){
                if (StringUtils.isNotEmpty(airport.getAirportCode())) {
                    List<SelectOptionDTO> selectOptionDTOS = JSON.parseArray(airport.getAirportCode(), SelectOptionDTO.class);
                    if (CollectionUtil.isNotEmpty(selectOptionDTOS)) {
                        airportList.addAll(selectOptionDTOS.stream().map(SelectOptionDTO::getValue).collect(Collectors.toSet()));
                    }
                }
            }
        }
        if(CollectionUtil.isNotEmpty(airportList)){
            for (String vo : airportList){
                SysUserAirport userAirport = new SysUserAirport();
                userAirport.setAirportCode(vo);
                sysUserAirports.add(userAirport);
            }
        }
        return sysUserAirports;

    }

    /**
     * 新增用户与机场关联
     *
     * @param sysUserAirport 用户与机场关联
     * @return 结果
     */
    @Override
    public int insertSysUserAirport(SysUserAirport sysUserAirport) {
        return sysUserAirportMapper.insertSysUserAirport(sysUserAirport);
    }

    /**
     * 修改用户与机场关联
     *
     * @param sysUserAirport 用户与机场关联
     * @return 结果
     */
    @Override
    public int updateSysUserAirport(SysUserAirport sysUserAirport) {
        return sysUserAirportMapper.updateSysUserAirport(sysUserAirport);
    }

    /**
     * 批量删除用户与机场关联
     *
     * @param userIds 需要删除的用户与机场关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserAirportByUserIds(Long[] userIds) {
        return sysUserAirportMapper.deleteSysUserAirportByUserIds(userIds);
    }

    /**
     * 删除用户与机场关联信息
     *
     * @param userId 用户与机场关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserAirportByUserId(Long userId) {
        return sysUserAirportMapper.deleteSysUserAirportByUserId(userId);
    }
}
